package config

import (
	"fmt"
	"log"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

func GetDsn() string {
	databses := beego.AppConfig.DefaultString("db::Database", "user")
	h := beego.AppConfig.DefaultString("db::Host", "127.0.0.1")
	u := beego.AppConfig.DefaultString("db::User", "root")
	pd := beego.AppConfig.DefaultString("db::Password", "123456")
	pt := beego.AppConfig.DefaultString("db::Port", "3306")
	chart := beego.AppConfig.DefaultString("db::Charset", "utf8mb4")
	local := beego.AppConfig.DefaultString("db::Loc", "Local")
	partime := beego.AppConfig.DefaultString("db::ParseTime", "true")

	// DSN=golang:zxc,./123@tcp(43.139.115.142:3306)/user?charset=utf8mb4&loc=Local&parseTime=true
	return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&loc=%s&parseTime=%s", u, pd, h, pt, databses, chart, local, partime)

}

var UserOrmer orm.Ormer

func InitUserOrmer() {
	driverName := "mysql"
	// dsn := config.GetDsn()
	// DSN=golang:zxc,./123@tcp(43.139.115.142:3306)/user?charset=utf8mb4&loc=Local&parseTime=true

	orm.RegisterDriver(driverName, orm.DRMySQL)
	if err := orm.RegisterDataBase("default", "mysql", GetDsn()); err != nil {
		log.Fatal(err)

	}
	orm.RunSyncdb("default", false, true)
	UserOrmer = orm.NewOrm()
}
